//
//  TKYMemberController.m
//  StationHouseHelper
//
//  Created by apple on 2018/6/21.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYMemberController.h"
#import "TKYMineCell.h"
#import "TKYMineDetaileController.h"
#import "DataModel.h"

@interface TKYMemberController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation TKYMemberController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    //添加成员信息列表
    [self.view addSubview:self.tableView];
    self.tableView.hidden = NO;
    
    NSArray *testArr = @[@"张三",@"李四",@"王五",@"赵六",@"田七",@"王小二",@"阿三"];
    NSMutableArray * personArray = [NSMutableArray arrayWithCapacity:testArr.count];
    for (NSString * name in testArr)
    {
        DataModel * model = [DataModel new];
        model.aName = name;
        [personArray addObject:model];
    }
    NSArray * tempArray = [self sringSectioncompositor:personArray withSelector:@selector(aName)isDeleEmptyArray:YES];
    self.tableData = tempArray[0];
    self.tableIndexData = tempArray[1];
}

//将传进来的对象按通讯录那样分组排序，每个section中也排序  dataarray是中存储的是一组对象，selector是属性名
- (NSArray *)sringSectioncompositor:(NSArray *)dataArray withSelector:(SEL)selector isDeleEmptyArray:(BOOL)isDele
{
    //    UILocalizedIndexedCollation是苹果贴心为开发者提供的排序工具，会自动根据不同地区生成索引标题
    UILocalizedIndexedCollation  *collation = [UILocalizedIndexedCollation currentCollation];
    
    NSMutableArray * indexArray = [NSMutableArray arrayWithArray:collation.sectionTitles];
    
    NSUInteger sectionNumber = indexArray.count;
    //建立每个section数组
    NSMutableArray * sectionArray = [NSMutableArray arrayWithCapacity:sectionNumber];
    for (int n = 0; n < sectionNumber; n++)
    {
        NSMutableArray *subArray = [NSMutableArray array];
        [sectionArray addObject:subArray];
    }
    
    for (DataModel *model in dataArray)
    {
        // 根据SEL方法返回的字符串判断对象应该处于哪个分区
        NSInteger index = [collation sectionForObject:model collationStringSelector:selector];
        NSMutableArray *tempArray = sectionArray[index];
        [tempArray addObject:model];
    }
    
    for (NSMutableArray *tempArray in sectionArray)
    {
        //根据SEL方法返回的string对数组元素排序
        NSArray* sorArray = [collation sortedArrayFromArray:tempArray collationStringSelector:selector];
        [tempArray removeAllObjects];
        [tempArray addObjectsFromArray:sorArray];
    }
    // 是否删除空数组
    if (isDele)
    {
        [sectionArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSArray *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.count == 0)
            {
                [sectionArray removeObjectAtIndex:idx];
                [indexArray removeObjectAtIndex:idx];
            }
        }];
    }
    //返回第一个数组为table数据源  第二个数组为索引数组
    return @[sectionArray, indexArray];
}

#pragma mark-- UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _searchActive ? _resultData.count : _tableData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _searchActive ? [_resultData[section] count] : [_tableData[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TKYMineCell *cell = [tableView dequeueReusableCellWithIdentifier:kMineCellID];
    cell.selectedBtn.hidden = YES;
     DataModel * m = _searchActive ? _resultData[indexPath.section][indexPath.row] : _tableData[indexPath.section][indexPath.row];
    
    cell.nameLabel.text = m.aName;
    return cell;
}

- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return _searchActive? _resultIndexData : _tableIndexData;
}

//返回当用户触摸到某个索引标题时列表应该跳至的区域的索引。
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
#pragma mark - Table View Delegate
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel * tempLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 20)];
    tempLab.text = _searchActive ? _resultIndexData[section] : _tableIndexData[section];
    tempLab.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0];
    return tempLab;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TKYMineDetaileController *mineDetaileVC = [[TKYMineDetaileController alloc]init];
    
    [self.navigationController pushViewController:mineDetaileVC animated:YES];
}

- (BaseTabelView *)tableView{
    if (_tableView == nil) {
        _tableView = [[BaseTabelView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 200) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"TKYMineCell" bundle:nil] forCellReuseIdentifier:kMineCellID];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
